<template>
  <div class="MonthlyStatement">
    <div class="search">
      <span>查询日期：</span>
      <el-date-picker
        v-model="date"
        type="daterange"
        range-separator="至"
        start-placeholder="开始日期"
        end-placeholder="结束日期"
        value-format="yyyyMMdd"
        @change="setDate"
      >
      </el-date-picker>
      <el-button type="primary" @click="searchData">查询</el-button>
      <el-button @click="reset">重置</el-button>
      <el-button icon="el-icon-setting" @click="customConfig"
        >客户配置参数</el-button
      >
      <el-button type="primary" @click="modelConfig">报价模板配置</el-button>
    </div>

    <div class="content">
      <el-table
        v-if="tableData"
        v-loading="loading"
        max-height="550"
        border
        :data="tableData && tableData.list && tableData.list.list"
        tooltip-effect="dark"
      >
        <el-table-column
          v-for="(t, n) in tableData.fields"
          :key="n"
          :prop="t.fields"
          :label="t.translate"
          :min-width="
            flexColumnWidth(t.translate, t.fields, tableData.list.list)
          "
          align="center"
        />
      </el-table>
      <Empty v-else />
    </div>

    <div class="footer">
      <pagination
        :current-page="searchDate.pageDate.pageNum"
        :page-size="searchDate.pageDate.pageSize"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>

    <div v-if="maskFlag" class="mask">
      <div class="table">
        <div class="flex">
          <div class="title">{{ title }}</div>
          <i class="el-icon-close" @click="close"></i>
        </div>
        <div class="custom">
          <div>
            <div v-if="!modelFlag">
              <span>客户名称：</span>
              <el-select
                v-model="customId"
                placeholder="请选择"
                filterable
                @change="getConfig"
              >
                <el-option
                  v-for="item in customList"
                  :key="item.value"
                  :label="item.clientName"
                  :value="item.clientName"
                >
                </el-option>
              </el-select>
            </div>
            <div>
              <span>报价模板：</span>
              <el-select
                v-model="type"
                placeholder="请选择"
                filterable
                @change="setType"
              >
                <el-option
                  v-for="item in typeList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                ></el-option>
              </el-select>
            </div>
          </div>
          <el-button v-if="modelFlag && type" type="primary" @click="add"
            >新增数据</el-button
          >
        </div>
        <t-table
          v-if="sTableData"
          row-key="id"
          :columns="visibleColumns"
          :data="sTableData"
          :editable-cell-state="editableCellState"
          :max-height="700"
          :loading="tloading"
          bordered
        />
      </div>
    </div>

    <el-dialog
      title="新增数据"
      :visible.sync="open"
      top="6vh"
      width="30%"
      append-to-body
    >
      <el-form
        ref="formData"
        :model="formData"
        :rules="rules"
        label-width="200px"
      >
        <el-form-item label="目的省份" prop="targetProvince">
          <el-input v-model="formData.targetProvince"></el-input>
        </el-form-item>
        <el-form-item label="目的城市" prop="targetCity">
          <el-input v-model="formData.targetCity"></el-input>
        </el-form-item>
        <el-form-item
          v-if="type == 1"
          label="0-3KG（元/票）"
          prop="zeroToThree"
        >
          <el-input v-model="formData.zeroToThree"></el-input>
        </el-form-item>
        <el-form-item
          v-if="type == 1"
          label="3KG以上首重1kg（元/票）"
          prop="overThreeFirstWeight"
        >
          <el-input v-model="formData.overThreeFirstWeight"></el-input>
        </el-form-item>
        <el-form-item
          v-if="type == 1"
          label="3KG以上续重（元/1kg）"
          prop="overThreeContinueWeight"
        >
          <el-input v-model="formData.overThreeContinueWeight"></el-input>
        </el-form-item>
        <el-form-item v-if="type == 2" label="0-1KG（元/票）" prop="zeroToOne">
          <el-input v-model="formData.zeroToOne"></el-input>
        </el-form-item>
        <el-form-item v-if="type == 2" label="1-3KG（元/票）" prop="oneToThree">
          <el-input v-model="formData.oneToThree"></el-input>
        </el-form-item>
        <el-form-item
          v-if="type == 2"
          label="3-5KG（元/票）"
          prop="threeToFive"
        >
          <el-input v-model="formData.threeToFive"></el-input>
        </el-form-item>
        <el-form-item v-if="type == 2" label="5-999KG（元/票）" prop="overFive">
          <el-input v-model="formData.overFive"></el-input>
        </el-form-item>
        <el-form-item v-if="type == 3" label="0-5KG（元/票）" prop="zeroToFive">
          <el-input v-model="formData.zeroToFive"></el-input>
        </el-form-item>
        <el-form-item
          v-if="type == 3"
          label="5KG以上首重1kg（元/票）"
          prop="overFiveFirstWeight"
        >
          <el-input v-model="formData.overFiveFirstWeight"></el-input>
        </el-form-item>
        <el-form-item
          v-if="type == 3"
          label="5KG以上续重（元/1kg）"
          prop="overFiveContinueWeight"
        >
          <el-input v-model="formData.overFiveContinueWeight"></el-input>
        </el-form-item>
        <el-form-item label="加收（元/kg）" prop="additionalChargeWeight">
          <el-input v-model="formData.additionalChargeWeight"></el-input>
        </el-form-item>
        <el-form-item label="加收（元/票）" prop="additionalChargeSheet">
          <el-input v-model="formData.additionalChargeSheet"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  getCollectList,
  getCustomIncome,
  editCustomIncome,
  addCustomIncome,
} from "@/api/statisticalCenter";
import { getkhlist } from "@/api/apifile/Networkfinance";
import { Input, MessagePlugin } from "tdesign-vue";

export default {
  name: "MonthlyStatement",
  data() {
    return {
      date: null,
      loading: true,
      searchDate: {
        endTime: "",
        pageDate: {
          pageNum: 1,
          pageSize: 10,
        },
        startTime: "",
      },
      total: 0,
      tableData: null,
      maskFlag: false,
      customList: null,
      customId: null,
      sTableData: null,
      tloading: false,
      typeList: [
        { label: "模板1", value: "1" },
        { label: "模板2", value: "2" },
        { label: "模板3", value: "3" },
      ],
      type: null,
      title: "客户报价表",
      modelFlag: false,
      open: false,
      formData: {
        targetProvince: "",
        targetCity: "",
        zeroToThree: "",
        overThreeFirstWeight: "",
        overThreeContinueWeight: "",
        zeroToOne: "",
        oneToThree: "",
        threeToFive: "",
        overFive: "",
        zeroToFive: "",
        overFiveFirstWeight: "",
        overFiveContinueWeight: "",
        additionalChargeWeight: "",
        additionalChargeSheet: "",
      },
      rules: {
        targetProvince: [
          { required: true, message: "请输入目的省份", trigger: "change" },
        ],
      },
    };
  },
  computed: {
    columns() {
      return [
        {
          title: "目的省份",
          colKey: "targetProvince",
          align: "center",
          edit: this.editMethod(),
          show: true,
        },
        {
          title: "目的城市",
          colKey: "targetCity",
          align: "center",
          edit: this.editMethod(),
          show: true,
        },
        {
          title: "0-3kg（元/票）",
          colKey: "zeroToThree",
          align: "center",
          edit: this.editMethod(),
          show: this.type == 1,
        },
        {
          title: "3.01kg以上",
          align: "center",
          show: this.type == 1,
          children: [
            {
              title: "首重1kg（元/票）",
              colKey: "overThreeFirstWeight",
              align: "center",
              edit: this.editMethod(),
              show: this.type == 1,
            },
            {
              title: "续重（元/1kg）",
              colKey: "overThreeContinueWeight",
              align: "center",
              edit: this.editMethod(),
              show: this.type == 1,
            },
          ],
        },
        {
          title: "0-1kg（元/票）",
          colKey: "zeroToOne",
          align: "center",
          edit: this.editMethod(),
          show: this.type == 2,
        },
        {
          title: "1-3kg（元/票）",
          colKey: "oneToThree",
          align: "center",
          edit: this.editMethod(),
          show: this.type == 2,
        },
        {
          title: "3-5kg（元/票）",
          colKey: "threeToFive",
          align: "center",
          edit: this.editMethod(),
          show: this.type == 2,
        },
        {
          title: "5-999kg（元/票）",
          colKey: "overFive",
          align: "center",
          edit: this.editMethod(),
          show: this.type == 2,
        },
        {
          title: "0-5kg（元/票）",
          colKey: "zeroToFive",
          align: "center",
          edit: this.editMethod(),
          show: this.type == 3,
        },
        {
          title: "5kg以上",
          align: "center",
          show: this.type == 3,
          children: [
            {
              title: "首重1kg（元/票）",
              colKey: "overFiveFirstWeight",
              align: "center",
              edit: this.editMethod(),
              show: this.type == 1,
            },
            {
              title: "续重（元/1kg）",
              colKey: "overFiveContinueWeight",
              align: "center",
              edit: this.editMethod(),
              show: this.type == 1,
            },
          ],
        },
        {
          title: "加收（元/kg）",
          colKey: "additionalChargeWeight",
          align: "center",
          edit: this.editMethod(),
          show: true,
        },
        {
          title: "加收（元/票）",
          colKey: "additionalChargeSheet",
          align: "center",
          edit: this.editMethod(),
          show: true,
        },
      ];
    },
    visibleColumns() {
      return this.columns.filter((column) => column.show);
    },
  },
  mounted() {
    this.getTableData();
    this.getCustomList();
  },
  methods: {
    add() {
      this.open = true;
    },
    submitForm() {
      this.$refs["formData"].validate((valid) => {
        if (valid) {
          this.formData.applicableCustomer = this.customId;
          this.formData.type = this.type;
          addCustomIncome(this.formData).then((res) => {
            this.$message.success("添加成功！");
            this.getCustomIncome();
            this.cancel();
          });
        }
      });
    },
    cancel() {
      this.open = false;
      this.formData = {};
    },
    modelConfig() {
      this.title = "模板配置";
      this.modelFlag = true;
      this.maskFlag = true;
    },
    setType(type) {
      this.type = type;
      if (this.modelFlag) {
        this.customId = "默认";
        this.getCustomIncome();
      } else {
        if (this.customId) {
          this.getCustomIncome();
        } else {
          this.$message.warning("请选择客户");
        }
      }
    },
    getCustomIncome() {
      this.tloading = true;
      getCustomIncome({
        applicableCustomer: this.customId,
        type: this.type,
      }).then((res) => {
        this.sTableData = res.data;
        this.tloading = false;
      });
    },
    editMethod() {
      return {
        component: Input,
        props: {
          clearable: true,
          autofocus: true,
        },
        abortEditOnEvent: ["onEnter"],
        onEdited: (context) => {
          editCustomIncome(context.newRowData).then((res) => {
            if (res.code == 200) {
              this.sTableData.splice(context.rowIndex, 1, context.newRowData);
              MessagePlugin.success("修改成功！");
              this.getCustomIncome();
            }
          });
        },
        rules: [{ required: true, message: "不能为空" }],
      };
    },
    editableCellState() {
      return true;
    },
    getConfig(id) {
      this.customId = id;
      if (this.type) {
        this.getCustomIncome();
      } else {
        this.$message.warning("请选择报价模板");
      }
    },
    async getCustomList() {
      const res = await getkhlist();
      this.customList = res.data;
    },
    customConfig() {
      this.title = "客户报价表";
      this.modelFlag = false;
      this.maskFlag = true;
    },
    close() {
      this.maskFlag = false;
      this.sTableData = null;
      this.customId = null;
      this.type = null;
    },
    setDate(date) {
      this.searchDate.startTime = date[0];
      this.searchDate.endTime = date[1];
    },
    getTableData() {
      this.loading = true;
      getCollectList(this.searchDate).then((res) => {
        this.tableData = res.data;
        this.total = res.data.list.total;
        this.loading = false;
      });
    },
    searchData() {
      if (this.date) {
        this.searchDate.pageDate.pageNum = 1;
        this.searchDate.pageDate.pageSize = 10;
        this.getTableData();
      } else {
        this.$message.warning("请选择查询时间！");
      }
    },
    reset() {
      this.date = null;
      this.searchDate.startTime = null;
      this.searchDate.endTime = null;
    },
    handleSizeChange(size) {
      this.searchDate.pageDate.pageSize = size;
      this.getTableData();
    },
    handleCurrentChange(current) {
      this.searchDate.pageDate.pageNum = current;
      this.getTableData();
    },
  },
};
</script>

<style lang="scss" scoped>
.MonthlyStatement {
  padding: 20px;
  .search {
    padding-bottom: 20px;
    border-bottom: 1px solid #ccc;
    .el-date-editor {
      margin-right: 20px;
      width: 400px;
    }
    span {
      font-size: 14px;
      color: #606266;
    }
  }
  .content {
    margin-top: 20px;
  }
  .footer {
    margin-top: 20px;
    display: flex;
    justify-content: flex-end;
  }
  .mask {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: 10;
    background: rgba($color: #000000, $alpha: 0.2);
    .flex {
      display: flex;
      justify-content: space-between;
      align-items: center;
      margin-bottom: 30px;
    }
    .title {
      padding-left: 12px;
      border-left: 6px solid skyblue;
      font-size: 24px;
      font-weight: bold;
    }
    i {
      font-size: 28px;
      font-weight: bold;
      cursor: pointer;
    }
    .table {
      margin: 80px auto;
      padding: 30px;
      width: 60%;
      border-radius: 24px;
      background-color: #fff;
      .custom {
        margin-bottom: 20px;
        padding: 0 20px;
        display: flex;
        justify-content: space-between;
        align-items: center;
        & > div {
          display: flex;
          & > div + div {
            margin-left: 30px;
          }
        }
      }
    }
    &::v-deep .t-table {
      .t-table__header {
        tr th {
          font-size: 16px;
          font-weight: bolder;
        }
      }
      .t-table__body {
        tr td {
          font-size: 16px;
          font-weight: bolder;
          color: #666;
        }
      }
    }
  }
}
</style>
